﻿
using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using DataCollection.WinForm.Forms;
using DataCollection.WinForm.ViewModel;
using Mapster;
using System.Drawing.Printing;

namespace DataCollection.WinForm
{
    public partial class ManageFormPointForm : DCBaseForm
    {
        #region 属性

        private FormPointViewModel viewModel = new FormPointViewModel(0);

        private FormPointItem? SelectedFormPointItem;

        #endregion

        #region 自定义方法
        /// <summary>
        /// 布局
        /// </summary>
        private void SetupForm()
        {
            ListTable.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Name","点位名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DevicePointNumber","设备点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("FormControlName","界面控件",AntdUI.ColumnAlign.Center),
            };
        }

        /// <summary>
        /// 列表
        /// </summary>
        public void LoadList()
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            List<FormPointModel> list = FormPointModel.List(where);
            List<FormPointItem> itemList = new List<FormPointItem>();
            foreach (FormPointModel model in list)
            {
                FormPointItem item = new FormPointItem();
                model.Adapt(item);
                itemList.Add(item);
            }
            ListTable.DataSource = itemList;
        }
        #endregion

        #region 系统事件
        public ManageFormPointForm()
        {
            InitializeComponent();
            Header.Text = "界面点位管理";
            FullScreen = true;
        }

        private void FormPointForm_Load(object sender, EventArgs e)
        {
            SetupForm();
            DCSpin(this, "加载数据...", (action) =>
            {
                LoadList();
            }, null, () => { });
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            AddFormPointForm frm = new AddFormPointForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        #endregion

        private void ListTable_CellClick(object sender, AntdUI.TableClickEventArgs e)
        {
            if (e.Record is FormPointItem data)
            {
                SelectedFormPointItem = data;
            }
        }
        /// <summary>
        /// 删除点位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PointDeleteButton_Click(object sender, EventArgs e)
        {
            if (SelectedFormPointItem == null)
            {
                Error("请选择点位");
                return;
            }

            string msg = string.Format("您确定要删除【{0}】点位吗？", SelectedFormPointItem.Name);
            AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
            {
                OnOk = config =>
                {
                    viewModel = new FormPointViewModel(long.Parse(SelectedFormPointItem.Number));
                    Response response = viewModel.Delete();
                    if (response.Code == Response.SuccessCode)
                    {
                        Success(response.Msg);
                        LoadList();
                    }
                    else
                    {
                        Error(response.Msg);
                    }
                    return true;
                },
            });
        }
    }
}
